const path = require('path')
const webpack = require('webpack')
const ThemeColorReplacer = require('webpack-theme-color-replacer')
const forElementUI = require('webpack-theme-color-replacer/forElementUI')

function resolve (dir) {
  return path.join(__dirname, dir)
}

// vue.config.js
module.exports = {
  configureWebpack: {
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      // 生成仅包含颜色的替换样式（主题色等）
      // TODO 需要增加根据环境不开启主题需求
      new ThemeColorReplacer({
        fileName: 'css/theme-colors-[contenthash:8].css',
        matchColors: forElementUI.getElementUISeries('#2d8cf0').concat('#fff'), // 主色系列
        // 改变样式选择器，解决样式覆盖问题
        changeSelector (selector) {
          switch (selector) {
            case '.ivu-calendar-today .ivu-calendar-date':
              return ':not(.ivu-calendar-selected-date):not(.ivu-calendar-selected-day)' + selector
            case '.ivu-btn:focus,.ivu-btn:hover':
              return '.ivu-btn:focus:not(.ivu-btn-primary),.ivu-btn:hover:not(.ivu-btn-primary)'
            case '.ivu-steps-item-process .ivu-steps-item-icon > .ivu-steps-icon':
              return ':not(.ivu-steps-item-process)' + selector
            case '.ivu-btn.active,.ivu-btn:active':
              return '.ivu-btn.active:not(.ivu-btn-primary),.ivu-btn:active:not(.ivu-btn-primary)'
            case '.ivu-menu-horizontal>.ivu-menu-item-active,.ivu-menu-horizontal>.ivu-menu-item-open,.ivu-menu-horizontal>.ivu-menu-item-selected,.ivu-menu-horizontal>.ivu-menu-item:hover,.ivu-menu-horizontal>.ivu-menu-submenu-active,.ivu-menu-horizontal>.ivu-menu-submenu-open,.ivu-menu-horizontal>.ivu-menu-submenu-selected,.ivu-menu-horizontal>.ivu-menu-submenu:hover':
            case '.ivu-menu-horizontal > .ivu-menu-item-active,.ivu-menu-horizontal > .ivu-menu-item-open,.ivu-menu-horizontal > .ivu-menu-item-selected,.ivu-menu-horizontal > .ivu-menu-item:hover,.ivu-menu-horizontal > .ivu-menu-submenu-active,.ivu-menu-horizontal > .ivu-menu-submenu-open,.ivu-menu-horizontal > .ivu-menu-submenu-selected,.ivu-menu-horizontal > .ivu-menu-submenu:hover':
              return '.ivu-menu-horizontal > .ivu-menu-item-active,.ivu-menu-horizontal > .ivu-menu-item-open,.ivu-menu-horizontal > .ivu-menu-item-selected,.ivu-menu-horizontal:not(.ivu-menu-dark) > .ivu-menu-item:hover,.ivu-menu-horizontal > .ivu-menu-submenu-active,.ivu-menu-horizontal > .ivu-menu-submenu-open,.ivu-menu-horizontal:not(.ivu-menu-dark) > .ivu-menu-submenu-selected,.ivu-menu-horizontal:not(.ivu-menu-dark) > .ivu-menu-submenu:hover'
            case '.ivu-menu-horizontal > .ivu-menu-item-selected > a':
              return ':not(.ivu-menu-horizontal)' + selector
            case '.ivu-menu-horizontal > .ivu-menu-item > a:hover':
              return ':not(.ivu-menu-horizontal)' + selector
            default :
              return selector
          }
        }
      })
    ]
  },

  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('src')) // key,value自行定义，比如.set('@@', resolve('src/components'))
      .set('_c', resolve('src/components'))

    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .oneOf('inline')
      .resourceQuery(/inline/)
      .use('vue-svg-icon-loader')
      .loader('vue-svg-icon-loader')
      .end()
      .end()
      .oneOf('external')
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: 'assets/[name].[hash:8].[ext]'
      })
  },

  css: {
    loaderOptions: {
      less: {
        javascriptEnabled: true
      }
    }
  },

  devServer: {
    // development server port 8000
    port: 8000
    // proxy: {
    //   '/api': {
    //     // target: 'https://mock.ihx.me/mock/5baf3052f7da7e07e04a5116/antd-pro',
    //     target: 'https://mock.ihx.me/mock/5baf3052f7da7e07e04a5116/antd-pro',
    //     ws: false,
    //     changeOrigin: true
    //   }
    // }
  },
  // disable source map in production
  productionSourceMap: false,
  lintOnSave: false,
  // babel-loader no-ignore node_modules/*
  transpileDependencies: []
}
